package org.example.controller.user;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.SobUser;
import org.example.response.ResponseResult;
import org.example.services.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserApi {

    @Autowired
    private IUserService userService;

    /**
     * 初始化管理员账号-init-admin
     *
     * @return
     */
    @PostMapping("/admin_account")
    public ResponseResult initManagerAccount(@RequestBody SobUser sobUser, HttpServletRequest request) {
        log.info("user name==>" + sobUser.getUserName());
        log.info("password==>" + sobUser.getPassword());
        log.info("email==>" + sobUser.getEmail());
        return userService.initManagerAccount(sobUser, request);
    }

    /**
     * 注册
     *
     * @param sobUser
     * @return
     */
    @PostMapping("/register")
    public ResponseResult register(@RequestBody SobUser sobUser, @RequestParam String emailCode, @RequestParam String captcha
            , @RequestParam String captchaKey, HttpServletRequest request) {
        return userService.register(sobUser, emailCode, captcha, captchaKey, request);
    }

    /**
     * 登录sign-up
     * <p>
     * 需要提交的数据
     * 1.用户账户-可以昵称，可以邮箱--->做了唯一处理
     * 2.密码
     * 3.图灵验证码
     * 4.图灵验证码key
     *
     * @param captcha    图灵验证码
     * @param captchaKey 图灵验证码key
     * @param sobUser    用户Bean类，封装着账号和密码
     * @return
     */
    @PostMapping("/{captcha}/{captcha_key}")
    public ResponseResult login(@PathVariable("captcha") String captcha,
                                @PathVariable("captcha_key") String captchaKey,
                                @RequestBody SobUser sobUser,
                                HttpServletRequest request,
                                HttpServletResponse response) {
        return userService.doLogin(captcha, captchaKey, sobUser, request, response);
    }

    /**
     * 获取图灵验证码
     * 有效时长为10分组
     *
     * @return
     */
    @GetMapping("/captcha")
    public void getCaptcha(HttpServletResponse response, @RequestParam String captchaKey) {
        try {
            userService.createCaptcha(response, captchaKey);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    /**
     * 发送邮件
     * 使用场景：注册，找回密码，修改邮箱（会输入新的邮箱）
     * 注册：如果已经注册过了，就提示说，改邮箱已经注册
     * 找回密码：如果没有注册过，提示该邮箱没有注册
     * 修改邮箱（新的邮箱）：如果已经注册了，提示该邮箱已经注册
     *
     * @param emailAddress
     * @return
     */
    @GetMapping("/verify_code")
    public ResponseResult sendVerifyCode(HttpServletRequest request, @RequestParam String type, @RequestParam String emailAddress) {
        log.info("email==>" + emailAddress);
        return userService.sendEmail(type, request, emailAddress);
    }

    /**
     * 修改密码Password
     *
     * @param sobUser
     * @return
     */
    @PutMapping("/password/{userId}")
    public ResponseResult updatePassword(@PathVariable("userId") String userId, @RequestBody SobUser sobUser) {
        return null;
    }

    /**
     * 获取作者信息user-info
     *
     * @param userId
     * @return
     */
    @PostMapping("/{userId}")
    public ResponseResult getUserInfo(@PathVariable("userId") String userId) {
        return null;
    }

    /**
     * 修改用户信息user-info
     *
     * @param sobUser
     * @return
     */
    @PutMapping("/{userId}")
    public ResponseResult updateUserInfo(@PathVariable("userId") String userId, @RequestBody SobUser sobUser) {
        return null;
    }

    /**
     * 获取用户列表
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ResponseResult listUser(@RequestParam("page") int page, @RequestParam("size") int size) {
        return null;
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/{userId}")
    public ResponseResult deleteUser(@PathVariable("userId") String userId) {
        return null;
    }
}
